package com.example.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.auth.SysMenu;
import com.example.entity.auth.SysRole;
import com.example.entity.dto.User.MyUser;
import com.example.entity.vo.auth.MesPermission;
import com.example.entity.vo.auth.YFPermission;
import com.example.mapper.UserMapper;
import com.example.service.auth.YFPermissionService;
import com.example.service.impl.auth.YFPermissionServiceImpl;
import com.example.untils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import com.example.service.auth.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@DS("mes")
@Service("userDetailsService")
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private YFPermissionService yfPermissionService;

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        YFPermission permissonOnYF = yfPermissionService.getPermissonOnYF(username);
        MesPermission mesPermission = yfPermissionService.getMesPermission(username);
//        System.out.println("permissonOnYF + " + TimeUtils.getNowTime());
//        System.out.println(permissonOnYF);
        //        System.out.println("loadUserByUsername loadUserByUsername.....");
        // 在易飞可以查出来得用户，才可以走到这里
//        Integer userId = userMapper.getUserIdByUsername(username);
//        if(userId == null) {
//            userMapper.insertSysUser(username);
//            userId = userMapper.getUserIdByUsername(username);
//        }

//        List<SysMenu> permission = userMapper.getPermission(userId.toString());
//        List<SysRole> role = userMapper.getRole(userId.toString());

        return new MyUser(username, userMapper.query(username) ,userMapper.getPwdById(username), permissonOnYF, mesPermission);
    }

//    @Autowired
//    private SysUserMapper mapper;
//
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_name" , username);
//        SysUser user = mapper.selectOne(wrapper);
//        if(user == null) {
//            throw new UsernameNotFoundException("用户名不存在");
//        }
//
//        Set<String> role = mapper.getRole(user.getUserId().toString());
//        Set<String> permission = mapper.getPermission(user.getUserId().toString());
//
////        List<GrantedAuthority> list = new ArrayList<>();
////        //授权(这里的授权只是用来骗过SpringSecurity框架的检验的，没有实际意义)
////        list.add(new SimpleGrantedAuthority("add"));
//
////        return new User(user.getUserName(), user.getPassword(), list);
//        return new MyUser(user.getUserName(), user.getPassword(), permission, role);
//    }
}
